#include <47func.h>
#include <mysql/mysql.h>

// Consume that 2 users have logged in: A and B

int main()
{
    // Initialization
    MYSQL *db = mysql_init(NULL);
    const char *host = "localhost";
    const char *user = "root";
    const char *password = "";
    const char *database = "nd_test";

    // Connection
    MYSQL *ret = mysql_real_connect(db, host, user, password, database, 0, NULL, 0);
    if(ret == NULL)
    {
        printf("Error:%s\n", mysql_error(db));
        return -1;
    }

    // getfile filename
    // char temp[1024] = {0};
    // char* username = "A";
	// char* path = "root/dir1/file1";

	// sprintf(temp,"select md5 from vftable where username = '%s' and path = '%s' and fileType = '1' and tomb = '0';", username, path);
	// char sql[1024] = {0};
	// strcat(sql, temp);
	// //printf("%s\n", sql);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    // // 获取表中的md5值
    // char md5[33] = {0};
    // MYSQL_RES *table = mysql_store_result(db);
    // printf("Total rows: %lu\n", mysql_num_rows(table));
    // MYSQL_ROW row;
    // while((row = mysql_fetch_row(table)) != NULL){
    //     for(unsigned int i = 0; i < mysql_num_fields(table); ++i){
    //         strcat(md5, row[i]); // 取出表里存的md5值
    //         //printf("%s\t", row[i]);
    //     }
    //     //printf("\n");
    // }

    /*--------------------------------------------------------------------------------*/

    // putfile filename
    // char sentence[1024] = {0};
    // char* md5 = "f123";
    // char* username = "B";
    // char* path = "root/dir4/file5";
    // char* preDir = "dir4";
    // char* filename = "file5";
    
    // sprintf(sentence, "insert into vftable (md5, username, path, fileType, preDir, tomb, filename) values ('%s', '%s', '%s', '1', '%s', '0', '%s');", md5, username, path, preDir, filename);
    // char sql[1024] = {0};
    // strcat(sql, sentence);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    /*--------------------------------------------------------------------------------*/

    // remove filename
    // char sentence[1024] = {0};
    // char* username = "B";
    // char* path = "root/dir4/file5";
    
    // sprintf(sentence, "update vftable set tomb = '1' where username = '%s' and path = '%s' and fileType = '1' and tomb = '0';", username, path);
    // char sql[1024] = {0};
    // strcat(sql, sentence);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    /*-------------------------------------------------------------------------------*/

    // mkdir dir
    // char sentence[1024] = {0};
    // char* username = "B";
    // char* path = "root/dir3";
    // char* preDir = "root";
    // char* filename = "dir3";
    
    // sprintf(sentence, "insert into vftable (username, path, fileType, preDir, tomb, filename) values ('%s', '%s', '0', '%s', '0', '%s');", username, path, preDir, filename);
    // char sql[1024] = {0};
    // strcat(sql, sentence);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    /*-------------------------------------------------------------------------------*/

    // rmdir dir
    // char sentence[1024] = {0};
    // char* username = "B";
    // char* path = "root/dir3";
    
    // sprintf(sentence, "update vftable set tomb = '1' where username = '%s' and path = '%s' and fileType = '0' and tomb = '0';", username, path);
    // char sql[1024] = {0};
    // strcat(sql, sentence);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    /*---------------------------------------------------------------------------------*/

    // cd path
    // char sentence[1024] = {0};
    // char* username = "A";
    // char* path = "root/dir1";
    
    // sprintf(sentence, "select path from vftable where username = '%s' and path = '%s' and fileType = '0' and tomb = '0';", username, path);
    // char sql[1024] = {0};
    // strcat(sql, sentence);

    // int qret = mysql_query(db, sql);
    // if(qret != 0)
    // {
    //     printf("SQL error:%s\n", mysql_error(db));
    //     return -1;
    // }

    // // 获取表中的path
    // char md5[1024] = {0};
    // MYSQL_RES *table = mysql_store_result(db);
    // printf("Total rows: %lu\n", mysql_num_rows(table));
    // MYSQL_ROW row;
    // while((row = mysql_fetch_row(table)) != NULL)
    // {
    //     for(unsigned int i = 0; i < mysql_num_fields(table); ++i)
    //     {
    //         strcat(md5, row[i]); // 取出表里存的md5值
    //         printf("%s\t", row[i]);
    //     }
    //     printf("\n");
    // }

    /*---------------------------------------------------------------------------------*/

    // ls
    char sentence[1024] = {0};
    char* username = "A";
    char* preDir = "root";
    
    sprintf(sentence, "select filename from vftable where username = '%s' and preDir = '%s' and tomb = '0' and filename <> 'NULL';", username, preDir);
    char sql[1024] = {0};
    strcat(sql, sentence);

    int qret = mysql_query(db, sql);
    if(qret != 0)
    {
        printf("SQL error:%s\n", mysql_error(db));
        return -1;
    }

    // 获取表中的filename
    char result[2048] = {0};
    MYSQL_RES *table = mysql_store_result(db);
    printf("Total rows: %lu\n", mysql_num_rows(table));
    MYSQL_ROW row;
    while((row = mysql_fetch_row(table)) != NULL)
    {
        for(unsigned int i = 0; i < mysql_num_fields(table); ++i)
        {
            strcat(result, row[i]); // 取出表里存的md5值
            printf("%s\t", row[i]);
        }
        printf("\n");
    }

    
    mysql_close(db);

    return 0;
}